<?php
/***
 * 描述：商品价格视图
 * @category		Lib
 * @package			Model			
 * @subpackage	
 * @author			wanght <whtaok@gmail.com>
 * @since			2013-7-30
 * @version			$Id: ProductPriceViewModel.class.php 73 2013-12-01 12:13:16Z whtaok@gmail.com $
 */
class ProductPriceViewModel extends CommonViewModel{
	//商品价格模型字段
	public $productPriceViewFields = array(
			'id'			=>'id',
			'shop_id'		=>'shop_id',
			'price_real'	=>'price_real',
			'price_sale'	=>'price_sale',
			'price_vip'		=>'price_vip',
			'stock_qty'		=>'stock_qty',
			'stock_warn_qty'	=>'stock_warn_qty',
			'is_show'		=>'is_show',
			'is_hot'		=>'is_hot',
			'is_recommend'	=>'is_recommend',
			'is_new'		=>'is_new',
			'is_foreign'	=>'is_foreign',
			'ordlist'		=>'ordlist',
			'upd_time'		=>'upd_time',
		);
	//商品基本表模型字段
	public $productViewFields = array(
			'id'			=>'prod_id',
			'name'			=>'name',
			'title'			=>'title',
			'cate_code1'	=>'cate_code1',
			'cate_code2'	=>'cate_code2',
			'spec'			=>'spec',
			'spec_unit'		=>'spec_unit',
			'spec_qty'		=>'spec_qty',
			'barcode'		=>'barcode',
			'price_sale'	=>'base_price_sale',
			'price_vip'		=>'base_price_vip',
			'stock_warn_qty'=>'base_stock_warn_qty',
			'is_show'		=>'base_is_show',
			'is_hot'		=>'base_is_hot',
			'is_recommend'	=>'base_is_recommend',
			'ordlist'		=>'base_ordlist',
	);
	//查询已经设置过价格商品列表
	public function buildPricedFields($shop_id){
		
		$productPriceViewFields = $this->productPriceViewFields;
		$productViewFields = $this->productViewFields;
		$productViewFields['_on'] = 'ProductPrice.prod_id = ProductLib.id and ProductPrice.shop_id = '.$shop_id;
		$this->viewFields=array(
			'ProductPrice' => $productPriceViewFields,
			'ProductLib' 	=>$productViewFields
		);
		return $this;
	}
	//查询未设置过价格的商品列表
	public function buildUnpricedFields($shop_id){
		$productViewFields = $this->productViewFields;
		$productViewFields['_type'] = 'LEFT';
		$productPriceViewFields = $this->productPriceViewFields;
		$productPriceViewFields['_on'] = 'ProductPrice.prod_id = ProductLib.id and ProductPrice.shop_id = '.$shop_id;
		$this->viewFields=array(
				'ProductLib' 	=>$productViewFields,
				'ProductPrice' => $productPriceViewFields
		);
		return $this;
	}
	/**
	 * 描述：根据查询条件，查询指定商铺的商品详细信息列表，带分页
	 * @access		public
	 * @author		wanght<whtaok@gmail.com>
	 * @param 		string $shop_id		商铺ID
	 * @param 		array  $search		商品查询条件
	 * @param 		string $order		排序
	 * @since		2013-4-21
	 */
	public function findListPage($shop_id,$search,$order='is_hot desc,is_recommend desc,ordlist'){
		//上架商品
		if($search['is_show']==1){
			$modWhere['is_show'] = 1;
		}else if($search['is_show']==0){
			$modWhere['is_show'] = 0;
		}
		//分类条件
		if($search['cate_code']){
			$modWhere['cate_code2'] = array('like',$search['cate_code'].'%');
		}
		//商品名称
		if($search['name']){
			$modWhere['name'] = array('like','%'.$search['name'].'%');
		}
		//商品代码
		if($search['brand']){
			$modWhere['brand'] = array('like','%'.$search['brand'].'%');
		}
		//新商品
		if($search['is_new']){
			$modWhere['is_new'] = 1;
		}
		//进口商品
		if($search['is_foreign']){
			$modWhere['is_foreign'] = 1;
		}
		//热门商品
		if($search['is_hot']){
			$modWhere['is_hot'] = 1;
		}
		//推荐商品
		if($search['is_recommend']){
			$modWhere['is_recommend'] = 1;
		}
		$this->buildPricedFields($shop_id);
		$totalRows = $this->setModWhere($modWhere)->count();
		$rtnData = $this->setModWhere($modWhere)->order($order)->listPage($totalRows);
		return $rtnData;
	}
	/**
	 * 描述：根据查询条件，查询指定商铺的未定价商品详细信息列表，带分页 
	 * @access		public
	 * @author		wanght<whtaok@gmail.com>
	 * @param 		string $shop_id		商铺ID
	 * @param 		array  $search		商品查询条件
	 * @param 		string $order		排序
	 * @since		2013-4-21
	 */
	public function findUnpricedListPage($shop_id,$search,$order='base_ordlist'){
		//设置条件
		$modWhere = array(
				'base_is_show'=>1,
				'id'=>array('exp','is null')
		);
		//分类条件
		if($search['cate_code']){
			$modWhere['cate_code2'] = array('like',$search['cate_code'].'%');
		}
		//商品名称
		if($search['name']){
			$modWhere['name'] = array('like','%'.$search['name'].'%');
		}
		//商品代码
		if($search['brand']){
			$modWhere['brand'] = array('like','%'.$search['brand'].'%');
		}
		$this->buildUnpricedFields($shop_id);
		$totalRows = $this->setModWhere($modWhere)->count();
		$rtnData = $this->setModWhere($modWhere)->order($order)->listPage($totalRows);
		return $rtnData;
	}
	
	/**
	 * 描述：查询指定商铺，热门/推荐商品列表
	 * @access		public
	 * @author		wanght<whtaok@gmail.com>
	 * @param 		string 	$shop_id		商铺ID
	 * @param 		int		$order_status	订单状态
	 * @param 		array	$search			其他搜素条件
	 * @param 		string 	$ordlist		排序
	 * @since		2013-4-21
	 */
	public function findActiveStatusListPage($shop_id,$status_field,$cate_code=''){
		$this->buildPricedFields($shop_id);
		$modWhere[$status_field] = 1;
		if($cate_code){
			$modWhere['cate_code2'] = array('like',$cate_code.'%');
		}
		$modWhere['is_show'] = 1;
		$totalRows = $this->where($modWhere)->count();
		$rtnData = $this->where($modWhere)->order('ordlist')->listPage($totalRows);
		return $rtnData;
	}
	
}

?>